package com.tokenmaster.aitrade.trade.boll;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.log4j.Logger;

import com.tokenmaster.aitrade.common.system.KLinePoint;
import com.tokenmaster.aitrade.data.SystemDB;

/**
 * @Description: 生成布林线
 * @author buxinyun
 * @date 2018年8月23日
 *
 */
public class Create_Boll {

	public static void main(String[] args) {

		Logger logger = Logger.getLogger(Create_Boll.class);
		SystemDB systemDB = SystemDB.getInstance();
		Index_boll index_boll = new Index_boll();

		String symbol = "btc_usdt";
		String typeStr = "15min";

		// 获取最后一条记录
		KLinePoint last_KLinePoint = systemDB.getLastKLinePoint(symbol, typeStr);

		SimpleDateFormat sformat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
		String timestr = sformat.format(last_KLinePoint.getUnixTime());
		logger.info("获取到最后一条记录时间为:" + timestr);

		while (true) {
			try {
				// 获取之前的20条记录，KLinePoints
				List<KLinePoint> kLinePoints = systemDB.getKLinePointsBefore(symbol, typeStr,
						last_KLinePoint.getUnixTime(), 20);

				logger.info("计算:" + sformat.format(last_KLinePoint.getUnixTime()));
				Index_Boll_Info index_Boll_Info = index_boll.calculation(kLinePoints);
				
				systemDB.insertIndexBollInfo(symbol,typeStr, index_Boll_Info);

				if (kLinePoints.size() > 2)
					last_KLinePoint = kLinePoints.get(kLinePoints.size() - 2);

				Thread.sleep(1);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
